package ve.gob.ine.censo.beans.convert;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import ve.gob.ine.censo.model.sis_reportes.Rol;

@FacesConverter(value = "rolConverter")
public class RolConverter implements Converter {
	
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		
        List<Rol> roles = ((DualListModel) ((PickList) arg1).getValue()).getTarget();
        roles.addAll(((DualListModel) ((PickList) arg1).getValue()).getSource());
        
        for (Rol rol : roles) {
            if (arg2.equals(String.valueOf(rol.getId()))) {
                return rol;
            }
        }

        return null;
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        return String.valueOf(((Rol) arg2).getId());
	}
}